package org.hsieh.tr.explore.ui;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;
import org.hsieh.tr.explore.common.ui.AnnotationContentProvider;
import org.hsieh.tr.explore.common.ui.AnnotationLabelProvider;
import org.hsieh.tr.explore.dbsource.DatabaseInfoManager;
import org.hsieh.tr.explore.dbsource.IDatabaseInfo;
import org.hsieh.tr.explore.util.JFaceUtil;

/**
 * 
 * @author xiaowei.ye xiaowei.ye@soyatec.com
 * 
 */
public class DatabaseViewer extends ViewPart {

    public static final String ID = "org.hsieh.tr.explore.ui.DatabaseViewer";

    private TreeViewer treeViewer;

    @Override
    public void createPartControl(Composite parent) {

	GridLayoutFactory.swtDefaults().margins(0, 0).numColumns(1).applyTo(
		parent);

	createTreeViewer(parent);
    }

    private void createTreeViewer(Composite parent) {
	treeViewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.SINGLE);
	treeViewer.setContentProvider(new AnnotationContentProvider());
	treeViewer.setLabelProvider(new AnnotationLabelProvider());

	treeViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
	treeViewer.addDoubleClickListener(new IDoubleClickListener() {

	    public void doubleClick(DoubleClickEvent event) {
		editSelectedInfo();
	    }
	});

	initViewerInput();

	registerPopupMenu();
    }

    private void registerPopupMenu() {
	MenuManager popupMenuManager = new MenuManager("#PopupMenu");
	popupMenuManager.setRemoveAllWhenShown(true);
	Menu popupMenu = popupMenuManager.createContextMenu(treeViewer
		.getTree());
	treeViewer.getTree().setMenu(popupMenu);
	getSite().setSelectionProvider(treeViewer);
	getSite().registerContextMenu(popupMenuManager, treeViewer);
    }

    private void initViewerInput() {
	treeViewer.setInput(DatabaseInfoManager.INSTANCE.getInfos());
    }

    @Override
    public void setFocus() {
	treeViewer.getTree().setFocus();
    }

    public void refresh() {
	treeViewer.refresh();
    }

    public void editSelectedInfo() {
	NewConnectionWizard wizard = new NewConnectionWizard();
	Object fristSelected = JFaceUtil.getFristSelected(treeViewer
		.getSelection());
	if (fristSelected instanceof IDatabaseInfo) {
	    wizard.setDatabaseInfo((IDatabaseInfo) fristSelected);
	    WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard);
	    if (dialog.open() == Dialog.OK) {
		refresh();
	    }
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object getAdapter(Class adapter) {
	if (adapter.equals(Viewer.class)) {
	    return treeViewer;
	}
	return super.getAdapter(adapter);
    }

}
